package com.gh.controller;

import com.gh.model.user.Patient;
import com.gh.result.Result;
import com.gh.service.PatientService;
import com.gh.util.AuthContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author Eric
 * @Date 2021/6/21 20:09
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/user/patient")
public class PatientApiController {

    @Autowired
    private PatientService patientService;

    /**
     * 获取当前用户下所有就诊人
     */
    @GetMapping("auth/findAll")
    public Result findAll(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> list = patientService.findAllByUserId(userId);
        return Result.ok(list);
    }
    /**
     * 添加就诊人
     */
    @PostMapping("auth/save")
    public Result savePatient(@RequestBody Patient patient,HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        return Result.ok();
    }
    /**
     * 根据id获取就诊人信息
     */
    @GetMapping("auth/get/{id}")
    public Result getPatient(@PathVariable Long id){
        Patient patient = patientService.getPatientById(id);
        return Result.ok(patient);
    }

    /**
     * 修改就诊人
     */
    @PutMapping("auth/update")
    public Result updatePatient(@RequestBody Patient patient){
        patientService.updateById(patient);
        return Result.ok();
    }

    /**
     * 删除就诊人
     */
    @DeleteMapping("auth/delete/{id}")
    public Result deleteById(@PathVariable Long id){
        patientService.removeById(id);
        return Result.ok();
    }
    /**
     * 根据就诊人id获取就诊人信息
     */
    @GetMapping("inner/get/{id}")
    public Patient getPatientById(@PathVariable Long id){
        Patient patient = patientService.getPatientById(id);
        return patient;
    }
}
